import json

from api.base_service import BaseService


class OrderSettingService(BaseService):

    def __init__(self):
        super().__init__()
        self.service_name = "OrderSettingService"

    def add(self, order_setting_list):
        """
        根据预约设置信息，添加预约信息
        :param order_setting_list: 列表-元素是OrderSetting对象  示例：[{"orderDate": "2021-11-30 01:01:01", "number": 101}]
        :return:添加成功，返回True， 添加失败，返回False
        """
        resp = self.dubbo_client.invoke(self.service_name, "add", order_setting_list)
        if resp == "null":
            return True
        else:
            return False

    def get_order_setting_by_month(self, month):
        """
        根据月份，查询预约信息
        :param month: 月份，字符串 示例 "2021-09"  "2021.09"
        :return:预约信息 [{'date': 3, 'number': 101, 'reservations': 0}]
        """
        resp = self.dubbo_client.invoke(self.service_name, "getOrderSettingByMonth", month)
        return json.loads(resp)

    def edit_number_by_date(self, order_setting):
        """
        根据日期修改预约信息
        :param order_setting: OrderSetting 对象，示例： {"orderDate": "2021-11-30 01:01:01", "number": 101}
        :return: 如果修改成功，返回True；如果修改失败，返回False
        """
        order_setting["class"] = "com.itheima.pojo.OrderSetting"
        resp = self.dubbo_client.invoke(self.service_name, "editNumberByDate", order_setting)
        if resp == "null":
            return True
        else:
            return False


if __name__ == '__main__':
    oss = OrderSettingService()
    order_setting_list = [{"orderDate": "2021-11-30 ", "number": 101}]
    result = oss.add(order_setting_list)
    print(result)
    print(type(result))

    result = oss.get_order_setting_by_month("2022-01")
    print(result)
    order_setting = {"orderDate": "2021-11-30 10:10", "number": 101}
    result = oss.edit_number_by_date(order_setting)
    print(result)